class Solution {
public:
    int minCost(string colors, vector<int>& neededTime) {
        int lastc = colors[0], lastt = neededTime[0];
        int res = 0;
        for (int i = 1; i < colors.size(); i++) {
            if (colors[i] == lastc) {
                res += min(neededTime[i], lastt);
                lastt = max(neededTime[i], lastt);
            }
            else {
                lastc = colors[i];
                lastt = neededTime[i];
            }
        }
        return res;
    }
};